﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
using YXQ.Common.Extension;
namespace YXQ.Dao
{

    public class BJDao
    {


        public BJDao()
        {

            Mapper.CreateMap<Y_BJ, BJModel>();
        }
        public List<Y_BJ> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_BJ.Count();

                return edm.Y_BJ.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public BJModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_BJ.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_BJ, BJModel>(query);
            }

        }



        public int Save(BJModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_BJ.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_BJ();

                    edm.AddToY_BJ(query);

                }


                query.ProjectId = obj.ProjectId;

                query.lx = obj.lx;

                query.lxother = obj.lxother;

                query.ky = obj.ky;

                query.kyother = obj.kyother;

                query.xzyj = obj.xzyj;

                query.xzyjother = obj.xzyjother;

                query.ydgh = obj.ydgh;

                query.ydghother = obj.ydghother;

                query.fasc = obj.fasc;

                query.fascother = obj.fascother;

                query.jsgc = obj.jsgc;

                query.jsgcother = obj.jsgcother;

                query.cssc = obj.cssc;

                query.csscother = obj.csscother;

                query.sgbt = obj.sgbt;

                query.sgbtother = obj.sgbtother;

                query.zj = obj.zj;

                query.zjother = obj.zjother;

                query.aj = obj.aj;

                query.ajother = obj.ajother;

                query.sgxk = obj.sgxk;

                query.sgxkother = obj.sgxkother;

                query.xffa = obj.xffa;

                query.xffaother = obj.xffaother;

                query.xffacs = obj.xffacs;

                query.xffacsother = obj.xffacsother;

                query.hbsc = obj.hbsc;

                query.hbscother = obj.hbscother;

                query.szsc = obj.szsc;

                query.szscother = obj.szscother;

                query.ylsc = obj.ylsc;

                query.ylscother = obj.ylscother;

                query.ghys = obj.ghys;

                query.ghysother = obj.ghysother;

                query.gtys = obj.gtys;

                query.gtysother = obj.gtysother;

                query.xfys = obj.xfys;

                query.xfysother = obj.xfysother;

                query.fljc = obj.fljc;

                query.fljcother = obj.fljcother;

                query.ylys = obj.ylys;

                query.ylysother = obj.ylysother;

                query.hbys = obj.hbys;

                query.hbysother = obj.hbysother;

                query.jgba = obj.jgba;

                query.jgbaother = obj.jgbaother;

                query.flhz = obj.flhz;

                query.flhzother = obj.flhzother;

                query.rff = obj.rff;

                query.rffother = obj.rffother;

                query.hppz = obj.hppz;

                query.hppzother = obj.hppzother;

                query.tsba = obj.tsba;

                query.tsbaother = obj.tsbaother;

                query.szys = obj.szys;

                query.szysother = obj.szysother;

                query.rfys = obj.rfys;

                query.rfysother = obj.rfysother;

                query.createUserid = obj.createUserid;

                query.createrealname = obj.createrealname;

                query.createusername = obj.createusername;

                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }
        }
        /// <summary>
        ///  
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BJModel GetOneByProjectId(int id,int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
               
                var query = from s in edm.Y_BJ.Where(s=>s.ProjectId==id) 
                                select s;
                if (reid >0)
                {
                    query = query.Where(s=>s.Id==reid);
                }

                var str=query.ToTraceString();
                return Mapper.Map<Y_BJ, BJModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }
        /// <summary>
        /// 获取最后一次修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BJModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_BJ.Where(s => s.ProjectId == id);
                

                return Mapper.Map<Y_BJ, BJModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }

        /// <summary>
        /// 获取所有的记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<BJModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query=edm.Y_BJ.Where(s => s.ProjectId == id);
               


                return Mapper.Map<List<Y_BJ>, List<BJModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_BJ.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

